package net.wanho.controller;


import lombok.SneakyThrows;
import net.wanho.po.Member;
import net.wanho.service.MemberService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.MemberVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.Map;

import lombok.RequiredArgsConstructor;



import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Api(tags = "03.会员表接口")
@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @ApiOperation("会员表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                              @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                              @ApiParam("查询条件") Member member){
        Map<String, Object> map = memberService.findPage(pageNum,pageSize,member);
        return AjaxResult.success(map);
    }

    @ApiOperation("根据主键查询会员表")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Serializable id){
        return AjaxResult.success(memberService.findById(id));
    }

    @ApiOperation("新增会员表")
    @PostMapping
    public AjaxResult add(@RequestBody Member member){
        memberService.add(member);
        return AjaxResult.success();
    }

    @ApiOperation("修改会员表")
    @PutMapping
    public AjaxResult modify(@RequestBody Member member){
        memberService.modify(member);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除会员表")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Serializable id){
        memberService.removeById(id);
        return AjaxResult.success();
    }

    @SneakyThrows
    @ApiOperation("导出")
    @GetMapping("/export")
    public void export(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                       @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                       @ApiParam("查询条件") Member member,
                       HttpServletResponse response){
        Workbook workbook = memberService.export(pageNum,pageSize,member);

        // 设置响应类型
        response.setContentType("application/vnd.ms-excel");
        // 设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=member.xlsx");

        workbook.write(response.getOutputStream());
    }

    @ApiOperation("/导入")
    @PostMapping("/import")
    public AjaxResult importExcel(MultipartFile file){
        memberService.importExcel(file);
        return AjaxResult.success();
    }
}
